﻿--[[	$Id: caelLib.lua 2630 2012-09-28 16:56:56Z sdkyron@gmail.com $	]]

local _, caelLib = ...
_G["caelLib"] = caelLib

local EventFrame = CreateFrame("Frame")
EventFrame:SetScript("OnEvent", function(self, event, ...)
	if type(self[event]) == "function" then
		return self[event](self, event, ...)
	end
end)

caelLib.playerClass = select(2, UnitClass("player"))
caelLib.playerName = UnitName("player")
caelLib.playerRealm = GetRealmName()
caelLib.iLvl = math.floor(GetAverageItemLevel("player"))

caelLib.utf8sub = function(string, index, dots)
	local bytes = string:len()
	if bytes <= index then
		return string
	else
		local length, currentIndex = 0, 1

		while currentIndex <= bytes do
			length = length + 1
			local char = string:byte(currentIndex)
			if char > 240 then
				currentIndex = currentIndex + 4
			elseif char > 225 then
				currentIndex = currentIndex + 3
			elseif char > 192 then
				currentIndex = currentIndex + 2
			else
				currentIndex = currentIndex + 1
			end

			if length == index then
				break
			end
		end

		if length == index and currentIndex <= bytes then
			return string:sub(1, currentIndex - 1)..(dots and "..." or "")
		else
			return string
		end
	end
end

caelLib.screenWidth, caelLib.screenHeight = string.match((({GetScreenResolutions()})[GetCurrentResolution()] or ""), "(%d+).-(%d+)")

caelLib.scales = {
	["720"] = { ["576"] = 0.65},
	["800"] = { ["600"] = 0.7},
	["960"] = { ["600"] = 0.84},
	["1024"] = { ["600"] = 0.89, ["768"] = 0.7},
	["1152"] = { ["864"] = 0.7},
	["1176"] = { ["664"] = 0.93},
	["1280"] = { ["800"] = 0.84, ["720"] = 0.93, ["768"] = 0.87, ["960"] = 0.7, ["1024"] = 0.65},
	["1360"] = { ["768"] = 0.93},
	["1366"] = { ["768"] = 0.93},
	["1440"] = { ["900"] = 0.84},
	["1600"] = { ["1200"] = 0.7, ["1024"] = 0.82, ["900"] = 0.93},
	["1680"] = { ["1050"] = 0.84},
	["1768"] = { ["992"] = 0.93},
	["1920"] = { ["1440"] = 0.7, ["1200"] = 0.84, ["1080"] = 0.93},
	["2048"] = { ["1536"] = 0.7},
	["2560"] = { ["1600"] = 0.84, ["1440"] = 0.93}, -- 1440 untested, suggested by Anj 0.83
}

local ScaleFix

caelLib.scale = function(value)
    return ScaleFix * math.floor(value / ScaleFix + 0.5)
end

EventFrame.ADDON_LOADED = function(self, event, addon)
	if addon ~= "caelLib" then
		return
	end
	
	if not caelDB then
		caelDB  = {}
	end
	
	local UIScale = caelDB.scale or caelLib.scales[screenWidth] and caelLib.scales[screenWidth][screenHeight] or min(2, max(0.64, 768/string.match(GetCVar("gxResolution"), "%d+x(%d+)")))

	ScaleFix = (768/tonumber(GetCVar("gxResolution"):match("%d+x(%d+)"))) / UIScale
	
	self:UnregisterEvent(event)
end
EventFrame:RegisterEvent("ADDON_LOADED")

EventFrame.UPDATE_FLOATING_CHAT_WINDOWS = function(self, event)
	caelDB.scale = floor(GetCVar("uiScale") * 100 + 0.5)/100
end
EventFrame:RegisterEvent("UPDATE_FLOATING_CHAT_WINDOWS")

caelLib.dummy = function() end

caelLib.kill = function(object)
    local objectReference = object
    if type(object) == "string" then
        objectReference = _G[object]
    else
        objectReference = object
    end
    if not objectReference then return end
    if type(objectReference) == "frame" then
        objectReference:UnregisterAllEvents()
    end
    objectReference.Show = caelLib.dummy
    objectReference:Hide()
end

local myChars = {
	["Illidan"] = { 
		["DRUID"] = {
			["Mahigan"] = true,
			["Moopheus"] = true,
			["Wachabe"] = true
		},
		["HUNTER"] = {
			["Bowslinger"] = true,
			["Caellian"] = true
		},
		["MAGE"] = {
			["Archmagus"] = true
		},
		["MONK"] = {
			["Shaohao"] = true
		},
		["PRIEST"] = {
			["Erebos"] = true
		},
		["ROGUE"] = {
			["Ragnuk"] = true
		},
		["WARRIOR"] = {
			["Exoskell"] = true,
			["Qiulong"] = true
		},
	},
}

local charListA = {
	["Illidan"] = {
		["DRUID"] = {
			["Kallysto"] = true,
			["Mahigan"] = true,
			["Mayari"] = true,
			["Moopheus"] = true,
			["Wachabe"] = true
		},
		["HUNTER"] = {
			["Bowslinger"] = true,
			["Caellian"] = true,
			["Callysto"] = true
		},
		["MAGE"] = {
			["Archmagus"] = true
		},
		["MONK"] = {
			["Myoho"] = true,
			["Shaohao"] = true
		},
		["PRIEST"] = {
			["Erebos"] = true,
			["Enheduanna"] = true
		},
		["ROGUE"] = {
			["Ragnuk"] = true
		},
		["SHAMAN"] = {
			["Pimicow"] = true
		},
		["WARRIOR"] = {
			["Exoskell"] = true,
			["Qiulong"] = true,
			["Valimaya"] = true
		},
	},
}

if myChars[caelLib.playerRealm] and myChars[caelLib.playerRealm][caelLib.playerClass] and myChars[caelLib.playerRealm][caelLib.playerClass][UnitName("player")] then
	caelLib.myChars = true
else
	myChars = nil
end

if charListA[caelLib.playerRealm] and charListA[caelLib.playerRealm][caelLib.playerClass] and charListA[caelLib.playerRealm][caelLib.playerClass][UnitName("player")] then
	caelLib.CharListA = true
else
	charListA = nil
end

if myChars or charListA then
	caelLib.locale = "frFR"
end

myChars = nil
charListA = nil